package com.gh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gh.client.HospitalFeignClient;
import com.gh.enums.OrderStatusEnum;
import com.gh.enums.PaymentStatusEnum;
import com.gh.enums.PaymentTypeEnum;
import com.gh.helper.HttpRequestHelper;
import com.gh.mapper.PaymentMapper;
import com.gh.model.order.OrderInfo;
import com.gh.model.order.PaymentInfo;
import com.gh.service.OrderService;
import com.gh.service.PaymentService;
import com.gh.vo.order.SignInfoVo;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Eric
 * @Date 2021/6/28 20:56
 * @Version 1.0
 */
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, PaymentInfo> implements PaymentService {
    @Autowired
    private OrderService orderService;
    @Autowired
    private HospitalFeignClient hospitalFeignClient;
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer status) {
        //根据订单id和支付类型查询支付表是否存在相同订单
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderInfo.getId());
        queryWrapper.eq("payment_type", status);
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            return;
        }
        //添加数据
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(status);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd") + "|" + orderInfo.getHosname() + "|" + orderInfo.getDepname() + "|" + orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        baseMapper.insert(paymentInfo);
    }

    @Override
    public void paySuccess(String out_trade_no, Map<String, String> map) {
        //根据订单编号和支付类型得到支付记录
        QueryWrapper<PaymentInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("out_trade_no",out_trade_no);
        queryWrapper.eq("payment_type", PaymentTypeEnum.WEIXIN.getStatus());
        PaymentInfo paymentInfo = baseMapper.selectOne(queryWrapper);
        //更新支付记录
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setOutTradeNo(map.get("transaction_id"));
        paymentInfo.setCallbackContent(map.toString());
        baseMapper.updateById(paymentInfo);
        //根据订单号得到订单信息
        OrderInfo orderInfo = orderService.getById(paymentInfo.getOrderId());
        //更新订单信息
        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
        orderService.updateById(orderInfo);
        //调用医院接口,同步进行更新
        SignInfoVo signInfoVo = hospitalFeignClient.getSignInfoVo(orderInfo.getHoscode());//获取密钥和路径
        Map<String,Object> mapParam=new HashMap<>();
        mapParam.put("hoscode",orderInfo.getHoscode());
        mapParam.put("hosRecordId",orderInfo.getHosRecordId());
        mapParam.put("timestamp", HttpRequestHelper.getTimestamp());
        String sign = HttpRequestHelper.getSign(mapParam, signInfoVo.getSignKey());
        mapParam.put("sign", sign);
        JSONObject object = HttpRequestHelper.sendRequest(mapParam, signInfoVo.getApiUrl() + "/order/updatePayStatus");

    }

    @Override
    public PaymentInfo getPaymentInfo(Long orderId, Integer paymentType) {
        QueryWrapper<PaymentInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        queryWrapper.eq("payment_type",paymentType);
        PaymentInfo paymentInfo = baseMapper.selectOne(queryWrapper);
        return paymentInfo;
    }
}
